﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Infrastructure.User;
using Kaiser.Models.InputModels;
using Kaiser.Models;
using Xoohoo.Models.InputModels;
using System.Collections;
using System.Data;

namespace Kaiser.Repositories.iBatis
{
    public class SysRepository:IRepository
    {
        private IBatisHepler IBatisHepler = null;
        public SysRepository()
        {
            IBatisHepler = new BatisHepler("SQLMap.SYS.config");
        }

        #region  PermissionService
        public Permission GetPermissionItem(Guid id)
        {
            var result = new Permission();
            return result;
        }
        public Permission GetPermissionItem(string name)
        {
            var result = new Permission();
            return result;
        }
        public List<Permission> GetPermissionList()
        {
            var result = new List<Permission>();
            return result;
        }
        public ModelResult Save(PermissionInput Input)
        {
            var result = new ModelResult();
            return result;
        }
        public ModelResult Save(IEnumerable<PermissionInput> Input)
        {
            var result = new ModelResult();
            return result;
        }
        #endregion

        #region RoleService
        public Role GetRoleItem(Guid roleID)
        {
            var result = new Role();
            Hashtable parms = new Hashtable();
            parms["roleID"] = roleID;
            result = (Role)IBatisHepler.ExcuteSelectObj("GetRoleItemByCode", parms);
            return result;
        }
        public Role GetRoleItem(string roleName)
        {
            var result = new Role();
            return result;
        }
        public List<RoleBasic> GetRoleList()
        {
            var result = new List<RoleBasic>();

            return result;
        }
        public ModelResult Save(RoleInput roleInput)
        {
            var result = new ModelResult();

            return result;
        }
        #endregion

        #region UserGroupService
        public UserGroup GetUserGroupItem(Guid userGroupID)
        {
            var result = new UserGroup();
            return result;
        }
        public UserGroup GetUserGroupItem(string userGroupName)
        {
            var result = new UserGroup();
            return result;
        }
        public List<UserGroup> GetUserGroupList(Guid? parentID = null)
        {
            var result = new List<UserGroup>();
            return result;
        }
        public ModelResult Save(UserGroupInput userGroupInput)
        {
            var result = new ModelResult();
            return result;
        }
        #endregion

        #region UserService
        public User GetUserItem(int userID)
        {
            var result = new User();
            return result;
        }
        public User GetUserItem(string username)
        {
            //var result=new User();
            Hashtable parms = new Hashtable();
            parms["username"] = username;
            //System.Data.DataTable dt= IBatisHepler.ExcuteSelect("GetUserItemByUserName", parms);
            //if (dt.Rows.Count >= 1)
            //{
            //    UserGroupBasic ugb = new UserGroupBasic();
            //    ugb.UserGroupID = Guid.NewGuid();
            //    ugb.UserGroupName="";
            //    ugb.IsSystem = true;
            //    result = new User(username, dt.Rows[0]["FullName"].ToString(), dt.Rows[0]["Email"].ToString(), dt.Rows[0]["Password"].ToString(), (UserStatus)Convert.ToInt32(dt.Rows[0]["AccountState"].ToString()), ugb);
            //}
            User result = IBatisHepler.ExcuteSelectObj("GetUserItemByUserName", parms) as User;
            result.Identity=new UserIdentity("",false,result.UserName);
            result.DisplayName = result.FullName;
            return result;
        }
        public User GetUserItem(string username, UserStatus status)
        {
            //var result = new User();
            Hashtable parms = new Hashtable();
            parms["username"] = username;
            //System.Data.DataTable dt = IBatisHepler.ExcuteSelect("GetUserItemByUserName", parms);
            //if (dt.Rows.Count >= 1)
            //{
            //    UserGroupBasic ugb = new UserGroupBasic();
            //    ugb.UserGroupID = Guid.NewGuid();
            //    ugb.UserGroupName = "";
            //    ugb.IsSystem = true;
            //    result = new User(username, dt.Rows[0]["FullName"].ToString(), dt.Rows[0]["Email"].ToString(), dt.Rows[0]["Password"].ToString(), (UserStatus)Convert.ToInt32(dt.Rows[0]["AccountState"].ToString()), ugb);
            //}
            User result = IBatisHepler.ExcuteSelectObj("GetUserItemByUserName", parms) as User;
            return result;
        }
        public bool IsExists(string username)
        {
            return true;
        }
        public bool IsExists(int userId)
        {
            return true;
        }
        public bool ValidateUser(int userId, string username)
        {
            return true;
        }
        public ModelResult SaveEdit(UserInputEdit userInput)
        {
            var result = new ModelResult();
            return result;
        }
        public ModelResult SaveAdd(UserInputAdd userInput)
        {
            var result = new ModelResult();
            return result;
        }
        public ModelResult Save(UserInput userInput)
        {
            var result = new ModelResult();
            return result;
        }
        public ModelResult SetPassword(int userID, string password)
        {
            var result = new ModelResult();
            return result;
        }

        public void SetPasswordByUserName(string username, string password)
        {
            Hashtable parms = new Hashtable();
            parms["username"] = username;
            parms["password"] = password;
            var result=IBatisHepler.ExcuteUpdate("SetPasswordByUserName", parms);
        }

        public bool Remove(int userID)
        {
            return true;
        }
        public bool ChangeStatus(int userID, UserStatus status)
        {
            return true;
        }
        public bool SignIn(Func<User> getUser, Action<User> afterSignIn)
        {
            User user = getUser();

            if (user != null ) //user.IsAuthenticated
            {
        
                if (afterSignIn != null)
                {
                    afterSignIn(user);
                }

                return true;
            }

            return false;
        }
        public void SignOut()
        {

        }

        #endregion

        #region IRepository 成员
        public DataGrid<UserBasic> GetUserBasicList(UserSearch criteria, PagingInfo pageingInfo)
        {
            var result = new DataGrid<UserBasic>();
            int totalCount = 0;
            result.TableResult = IBatisHepler.ExcuteSelect("GetUserBasicList", criteria, pageingInfo.Size, pageingInfo.Index + 1, out totalCount);
            result.total = totalCount;
            return result;
        }

        #endregion 
          
        public bool Remove(Guid id)
        {
            return true;
        }
        public bool Remove(IEnumerable<Guid> ids)
        {
            return true;
        }

        #region SystemManage
        #region 部门管理
        public void InsertOrg(Org model) 
        {
            var result = IBatisHepler.ExcuteInsert("InsertOrg", model);
        }

        public void UpdateOrg(Org model)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateOrg", model);
        }


        public DataGrid<Org> GetSyncOrgTree(string deptid)
        {
            var result = new DataGrid<Org>();

            result.TableResult = IBatisHepler.ExcuteSelect("GetSyncOrgTree", new { DeptID = deptid });
            
            return result;
        }

        public void InsertUser(UserBasic model)
        {
            var result = IBatisHepler.ExcuteInsert("InsertUser", model);
        }

        public void DeleteUser(string UserName)
        {
            var result = IBatisHepler.ExcuteDelete("DeleteUser", UserName);
        }

        public void UpdateUser(UserBasic mode)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateUser", mode);
        }

        #endregion
        #endregion

        
       
    }
}
